package com.carlinfo.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.users.dao.IPartnerDao;
import com.carlinfo.users.pojo.ACourse;
import com.carlinfo.users.pojo.APartner;
import com.carlinfo.users.service.IPartnerService;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;

@Service("partnerService")
public class PartnerServiceImpl implements IPartnerService {

	@Resource
	private IPartnerDao partnerDao;
	
	@Override
	public JSONObject saveOneService(APartner partner) {
		int res = partnerDao.saveOne(partner);
		JSONObject resultJSON = new JSONObject();
		if(res>0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("deffect", res);
			dataJSON.put("id", partner.getId());
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", 0);
			resultJSON.put("info", "成功");
			
		}else {
			resultJSON.put("code", -1);
			resultJSON.put("info", "失败");
		}
		
		return resultJSON;
	}

	@Override
	public JSONObject updateOneService(APartner partner) {
		int res = partnerDao.updateOne(partner);
		JSONObject resultJSON = new JSONObject();
		if(res>0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("deffect", res);
			dataJSON.put("id", partner.getId());
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", 0);
			resultJSON.put("info", "成功");
			
		}else {
			resultJSON.put("code", -1);
			resultJSON.put("info", "失败");
		}
		
		return resultJSON;
	}

	@Override
	public JSONObject deleteOneService(Map<String, Object> condMap) {
		int res = partnerDao.deleteOne(condMap);
		JSONObject resultJSON = new JSONObject();
		if(res>0) {
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("deffect", res);
			dataJSON.put("id", condMap.get("id"));
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", 0);
			resultJSON.put("info", "成功");
			
		}else {
			resultJSON.put("code", -1);
			resultJSON.put("info", "失败");
		}
		
		return resultJSON;
	}

	@Override
	public APartner findOnePartnerSercice(Map<String, Object> conMap) {
		APartner partner = partnerDao.findOne(conMap);
		return partner;
	}

	@Override
	public List<APartner> findCondPartnerList(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			Page<ACourse> page = PageMethod.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());//当前页  每页多少条
			List<APartner> partnerList = this.partnerDao.findCondList(condMap);
			/* 设置总记录数 */
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return partnerList;
		}
		return this.partnerDao.findCondList(condMap);
	}

}
